﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class SiteStatistics : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.SiteStatistics_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData(); 
            OnlineUser ou = (OnlineUser)Session["User"];
    
            
            if (ou.UserAccessLevel != ("5"))
            {
                Response.Redirect("~/Default.aspx");
            }

            // THIS IS TEMPORARY WORKAROUND TO THE PROBLEM THAT SQL EXPRESS DOES NOT SUPPORT
            // SENDING EMAILS DO NOT DELETE UNTIL WE ARE MOVING TO NORMAL SQL 
            if (!IsPostBack)
            {
                try
                {
                    EmailsLabel.Text = "Sent " + GND.SendPendingMails() + " pending emails";
                }
                catch { }
            }
        }

        protected string ReturnPercentage(object Num, object OutOf)
        {
            int NumInt = Int32.Parse(Num.ToString());
            int OutOfInt = Int32.Parse(OutOf.ToString());

            string result = ((NumInt * 100) / OutOfInt).ToString() + "%";
            return (result);
        }
        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void HideSource(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "hide")
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                GND.ExecNivdalSQL("SourceID", "Nivdal_HideTrafficSource", e.CommandArgument);
                TrafficSourcesGridview.DataBind();
            }
        }

        protected string FormatLink(object link)
        {
            int MaxLength = 60;
            if (link.ToString().Length < MaxLength)
                return link.ToString();
            return(link.ToString().Substring(0,MaxLength));
        }
    }
}